package com.example.visit

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.StatusBean
import com.example.base.entity.VisitBeanItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.RequestType
import com.example.base.mvi.UiState
import com.example.visit.adapter.VisitAdapter
import com.example.visit.adapter.VisitStatusAdapter
import com.example.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitIntent, VisitViewModel>() {
    override val binding by lazy {
        ActivityVisitBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<VisitViewModel>()
    val visitAdapter by lazy { VisitAdapter() }
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    var con=""
    var position=0
    override fun initView() {
        binding.visitStatusRv.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity, RecyclerView.HORIZONTAL, false)
        }
        val statusList = mutableListOf<StatusBean>(
            StatusBean("全部状态", true),
            StatusBean("待审核", false),
            StatusBean("已通过", false),
            StatusBean("驳回", false),
            StatusBean("已完成", false)
        )
        visitStatusAdapter.addData(statusList)

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            this.position=position
            visitStatusAdapter.data.forEach {
                it.isCheck = false
            }
            visitStatusAdapter.data.get(position).isCheck = true
            visitStatusAdapter.notifyDataSetChanged()

            if(position==0){
                vm.sendIntent(VisitIntent.getVisits)
            }else{
                vm.sendIntent(VisitIntent.getVisitByStatus(visitStatusAdapter.data.get(position).status))
            }
        }

        binding.visitRv.apply {
            adapter = visitAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity)
        }
        binding.visitAdd.setOnClickListener {
            ARouter.getInstance().build("/visit/VisitAddActivity").navigation()
        }

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/visit/VisitApproveActivity").withString("vid",visitAdapter.data.get(position).id.toString()).navigation()
        }

        binding.back.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<VisitBeanItem>
                                visitAdapter.apply {
                                    data.clear()
                                    addData(items)
                                    notifyDataSetChanged()
                                }
                                if(items.isNullOrEmpty()){
                                    vm.sendIntent(VisitIntent.getVisitByPurpose(con))
                                }
                            }

                            else -> {}
                        }
                    }

                    is UiState.onError -> {
                        Log.i("lr",it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(VisitIntent.getVisits)

    }
}